//
//  DataHandler.m
//  ForAddSDK
//
//  Created by admin on 2018/1/9.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "DataHandler.h"
#import <AppKit/NSPanel.h>

static DataHandler *handler;

@implementation DataHandler

+ (instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        handler = [DataHandler new];
        handler.isPathOK = NO;
    });
    return handler;
}

+ (void)refreshInit 
{
    handler = [DataHandler new];
    handler.isPathOK = NO;
}

- (NSArray *)getValueArray {
    return @[_svnPath,_projectSubPath,_pictureSubPath,_originPlatform,_workPlatform];
}

+ (NSString *)runScript:(NSString *)scriptName type:(NSString *)type launchPath:(NSString *)launchPath arguments:(NSArray *)arguments {
    //脚本路径
    NSString *shellPath = [[NSBundle mainBundle] pathForResource:scriptName ofType:type];
    return [DataHandler runScriptAtPath:shellPath launchPath:launchPath arguments:arguments];
}

+ (NSString *)runScriptAtPath:(NSString *)path launchPath:(NSString *)launchPath arguments:(NSArray *)arguments
{
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:launchPath];
    //数组index 0 shell路径, 如果shell 脚本有输入参数,可以加入数组里，index 1 可以输入$1 @[path,@"$1"],依次延后。
    NSMutableArray *arg = [NSMutableArray arrayWithObject:path];
    [arg addObjectsFromArray:arguments];
    [task setArguments: arg];
    NSPipe *pipe = [NSPipe pipe];
    task.standardOutput = pipe;
    task.standardError = pipe;
    NSFileHandle *file = [pipe fileHandleForReading];
    [task launch];
    [task waitUntilExit];
    NSData *data;
    data = [file readDataToEndOfFile];
    NSString *string = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
    return string;
}

+ (BOOL)exitPath:(NSString *)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    return [fileManager fileExistsAtPath:path];
}

+ (BOOL)isExistPlatformProject:(NSString *)platform
{
    return [DataHandler exitPath:[NSString stringWithFormat:@"%@%@/proj.ios_appstore_%@",handler.svnPath,handler.projectSubPath,platform]];
}

+ (BOOL)isexistPictureFolder:(NSString *)platform
{
    return [DataHandler exitPath:[NSString stringWithFormat:@"%@%@/%@",handler.svnPath,handler.pictureSubPath,platform]];
}

+ (NSMutableArray *)getDefaultValuesArray
{
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"defaultDataArray" ofType:@"plist"];
    NSMutableArray *data = [NSMutableArray arrayWithContentsOfFile:dataPath];
    return (data ? data : [NSMutableArray arrayWithCapacity:0]);
}

+ (void)saveDefaultValuesArray:(NSArray *)array
{
    NSString *dataPath = [[NSBundle mainBundle] pathForResource:@"defaultDataArray" ofType:@"plist"];
    NSArray *data = array ? array : [NSArray new];
    [data writeToFile:dataPath atomically:NO];
}

+ (NSDictionary *)getDefaultValuesWithGameID:(NSString *)gameID
{
    if (![gameID isEqualToString:@"none"]) {
        NSArray *data = [DataHandler getDefaultValuesArray];
        for (NSDictionary *dic in data) {
            if ([dic[COLUMNID_GAME_ID] isEqualToString:gameID]) {
                return dic;
            }
        }
    }
    return nil;
}

@end
